Completed
Push — master ( 40c2b4...09bd06 )
by Justin
01:29
created

Name.toJSON   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 3
loc 3
rs 10
1
var Conclusion = require('./Conclusion'),
2
    NameForm = require('./NameForm'),
3
    GDate = require('./Date'),
4
    utils = require('./utils');
5
6
/**
7
 * A name.
8
 * 
9
 * @constructor
10
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
11
 */
12
var Name = function(json){
13
  
14
  // Protect against forgetting the new keyword when calling the constructor
15
  if(!(this instanceof Name)){
16
    return new Name(json);
17
  }
18
  
19
  // If the given object is already an instance then just return it. DON'T copy it.
20
  if(Name.isInstance(json)){
21
    return json;
22
  }
23
  
24
  Conclusion.call(this, json);
25
  
26
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
27
    this.setType(json.type);
28
    this.setDate(json.date);
29
    this.setNameForms(json.nameForms);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
30
  }
31
};
32
33
Name.prototype = Object.create(Conclusion.prototype);
34
35
Name._gedxClass = Name.prototype._gedxClass = 'GedcomX.Name';
36
37
/**
38
 * Check whether the given object is an instance of this class.
39
 * 
40
 * @param {Object} obj
41
 * @returns {Boolean}
42
 */
43
Name.isInstance = function(obj){
44
  return utils.isInstance(obj, this._gedxClass);
45
};
46
47
/**
48
 * Get the name type
49
 * 
50
 * @returns {String} type
51
 */
52
Name.prototype.getType = function(){
53
  return this.type;
54
};
55
56
/**
57
 * Set the name type
58
 * 
59
 * @param {String} type
60
 * @returns {Name} This instance
61
 */
62
Name.prototype.setType = function(type){
63
  this.type = type;
64
  return this;
65
};
66
67
/**
68
 * Get the date
69
 * 
70
 * @returns {Date} date
71
 */
72
Name.prototype.getDate = function(){
73
  return this.date;
74
};
75
76
/**
77
 * Set the date
78
 * 
79
 * @param {Date|Object} date
80
 * @returns {Fact} This instance
81
 */
82
Name.prototype.setDate = function(date){
83
  if(date){
84
    this.date = GDate(date);
85
  }
86
  return this;
87
};
88
89
/**
90
 * Get the name forms
91
 * 
92
 * @return {NameForm[]}
93
 */
94
Name.prototype.getNameForms = function(){
95
  return this.nameForms || [];
96
};
97
98
/**
99
 * Set the name forms
100
 * 
101
 * @param {NameForm[]|Object[]} nameForms
102
 * @returns {Name} This instance
103
 */
104
Name.prototype.setNameForms = function(nameForms){
105
  return this._setArray(nameForms, 'nameForms', 'addNameForm');
106
};
107
108
/**
109
 * Add a name form
110
 * 
111
 * @param {NameForm|Object} nameForm
112
 * @returns {Name} This instance
113
 */
114
Name.prototype.addNameForm = function(nameForm){
115
  return this._arrayPush(nameForm, 'nameForms', NameForm);
116
};
117
118
/**
119
 * Export the object as JSON
120
 * 
121
 * @return {Object} JSON object
122
 */
123
Name.prototype.toJSON = function(){
124
  return this._toJSON(Conclusion, [
125
    'type',
126
    'date',
127
    'nameForms'
128
  ]);
129
};
130
131
module.exports = Name;